package com.jinkeen.xiaxie.keyboard

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.Toast
import com.jinkeen.xiaxie.R

/**
 * 仿支付宝支付密码键盘
 */
@SuppressLint("AppCompatCustomView")
class PasswordEdit : EditText {
    override fun setOnClickListener(l: OnClickListener?) {
        super.setOnClickListener(l)
        Toast.makeText(context, "sss", Toast.LENGTH_LONG).show()
    }

    /**
     * 画笔 一个密码所占宽度 密码的个数默认6位数 背景边框颜色
     * 背景边框大小 背景边框圆角大小 分割线的颜色 分割线的大小  密码圆点的颜色 密码圆点的半径大小
     */
    private var mPaint = Paint()
    private var mPasswordItemWidth = 0
    private var mPasswordNumber = 6
    private var mBgColor = Color.parseColor("#d1d2d6")
    private var mBgSize = 1
    private var mBgCorner = 0
    private var mDivisionLineSize = 1
    private var mDivisionLineColor = mBgColor
    private var mPasswordColor = mDivisionLineSize
    private var mPasswordRadius = 4

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(
        context,
        attributeSet
    ) {
        initPaint()
        initAttributeSet(context, attributeSet!!)
        //设置输入模式是密码
        inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
        //不显示光标
        isCursorVisible = false
    }

    private fun initPaint() {
        mPaint.isAntiAlias = true
        mPaint.isDither = true
    }

    private fun initAttributeSet(context: Context, attrs: AttributeSet) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText)
        mDivisionLineSize = array.getDimension(
            R.styleable.PasswordEditText_divisionLineSize,
            dip2x(mDivisionLineSize)
        ).toInt()
        mPasswordRadius = array.getDimension(
            R.styleable.PasswordEditText_passwordRadius,
            dip2x(mPasswordRadius)
        ).toInt()
        mBgSize =
            array.getDimension(R.styleable.PasswordEditText_divisionLineSize, dip2x(mBgSize))
                .toInt()
        mBgCorner = array.getDimension(R.styleable.PasswordEditText_bgCorner, 0f).toInt()
        //获取颜色
        mBgColor = array.getColor(R.styleable.PasswordEditText_bgColor, mBgColor)
        mDivisionLineColor =
            array.getColor(R.styleable.PasswordEditText_divisionLineColor, mDivisionLineColor);
        mPasswordColor =
            array.getColor(R.styleable.PasswordEditText_passwordColor, mDivisionLineColor);
        array.recycle()
    }

    private fun dip2x(dip: Int) = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        dip.toFloat(), getResources().getDisplayMetrics()
    )

    override fun onDraw(canvas: Canvas?) {
        var passwordWidth = width - (mPasswordNumber - 1) * mDivisionLineSize;
        mPasswordItemWidth = passwordWidth / mPasswordNumber
        //绘制背景
        drawBg(canvas!!)
        //绘制分割线
        drawDivisionLine(canvas)
        //绘制密码
        drawHidePassword(canvas)
    }

    private fun drawBg(canvas: Canvas) {
        mPaint.color = mBgColor
        //设置画笔为空心
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = mBgSize.toFloat()
        var rectF = RectF(
            mBgSize.toFloat(),
            mBgSize.toFloat(),
            (width - mBgSize).toFloat(),
            (height - mBgSize).toFloat()
        )
        //如果没有设置圆角，就画矩形
        if (mBgCorner == 0) {
            canvas.drawRect(rectF, mPaint)
        } else {
            //如果没有圆角就画矩形
            canvas.drawRoundRect(rectF, mBgCorner.toFloat(), mBgCorner.toFloat(), mPaint)
        }
    }

    private fun drawHidePassword(canvas: Canvas) {
        var passwordLength = text.length
        mPaint.color = mPasswordColor
        mPaint.style = Paint.Style.FILL
        for (i in 0 until passwordLength) {
            var cx =
                i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize
            canvas.drawCircle(
                cx.toFloat(),
                (height / 2).toFloat(),
                mPasswordRadius.toFloat(),
                mPaint
            )
        }
    }

    private fun drawDivisionLine(canvas: Canvas) {
        mPaint.strokeWidth = mDivisionLineSize.toFloat()
        mPaint.setColor(mDivisionLineColor)
        for (i in 0 until mPasswordNumber - 1) {
            var startX = (i + 1) * mDivisionLineSize + (i + 1) * mPasswordItemWidth + mBgSize
            canvas.drawLine(
                startX.toFloat(),
                mBgSize.toFloat(), startX.toFloat(), (height - mBgSize).toFloat(), mPaint
            )
        }
    }
}